﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MHPL_DoTrangSuc.Data;

namespace MHPL_DoTrangSuc.ManHinh
{
    public partial class frmSua_KhachHang : Form
    {
        KetNoi kn = new KetNoi();
        public frmSua_KhachHang()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát không ?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK)
            {
                this.Close();
            }
        }

        private void btnHuyBo_Click(object sender, EventArgs e)
        {
            cbGioiTinh.Text = "";
            txtMaKH.Text = "";
            
            foreach (Control ctr in this.groupThongTinKH.Controls)
            {
                if ((ctr is TextBox) || (ctr is DateTimePicker)||( ctr is ComboBox))
                {
                    ctr.Text = "";
                }
            }

        }

        private void listViewKH_SelectedIndexChanged(object sender, EventArgs e)
        {
                   
            int i = listViewKH.FocusedItem.Index;
            if (i < 0)
                return;
            txtMaKH.Text = listViewKH.Items[i].SubItems[5].Text;
            TxtHoTenKH.Text = listViewKH.Items[i].SubItems[0].Text;
            cbGioiTinh.Text = listViewKH.Items[i].SubItems[1].Text;
            txtEmail.Text = listViewKH.Items[i].SubItems[4].Text;
          
            TxtDienThoai.Text = listViewKH.Items[i].SubItems[3].Text;
            TxtDiaChi.Text = listViewKH.Items[i].SubItems[2].Text;
          
        
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                string update = "update KhachHang set HoTen=N'" + TxtHoTenKH.Text + "',GioiTinh=N'" + cbGioiTinh.Text + "',Email=N'" + txtEmail.Text + "',SoDT=N'" + TxtDienThoai.Text + "',DiaChi=N'" + TxtDiaChi.Text + "' where TenDN=N'" + txtMaKH.Text + "'";
                kn.thucthiketnoi(update);
                MessageBox.Show("Khách hàng đã được cập nhật thành công.", "Cập nhật Khách hàng");
                frmSua_KhachHang_Load(sender, e);


            }
            catch
            {
                MessageBox.Show("Dữ liệu đầu vào không đúng");
            }
            
        }

        private void frmSua_KhachHang_Load(object sender, EventArgs e)
        {
            kn.ketnoi();
            hienthi();
        }

        private void hienthi()
        {

            string str = "Select HoTen,GioiTinh,DiaChi,SoDT,Email,TenDn,MatKhau,TaiKhoan from KhachHang kh ";

            DataTable tb = new DataTable();
            tb.Load(kn.ThucHienReader(str));
            Load_ListView(listViewKH, tb);


        }
        public void Load_ListView(ListView lvwName, DataTable tb)
        {
            listViewKH.Items.Clear();
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                string[] col = new string[tb.Columns.Count];
                for (int j = 0; j < tb.Columns.Count; j++)
                {
                    col[j] = tb.Rows[i][j].ToString();
                }
                ListViewItem lvItem = new ListViewItem(col);
                lvwName.Items.Add(lvItem);
            }
        }

        
    }
}
